package com.itheima.shop.mapper;

import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import com.github.pagehelper.Page;
import com.itheima.shop.entity.Product;

public interface ProductMapper {
	@Select("select *  from tab_product where pid = #{pid}")
	@Results(id="productOne",value = {
			@Result(column = "cid", property = "category", one = @One(select = "com.itheima.shop.mapper.CategoryMapper.findByCid", fetchType = FetchType.LAZY)) })
	public Product findByPid(int pid);

	@Select("select tab_product.*,tab_category.cname from tab_product join tab_category on tab_product.cid = tab_category.cid where pid = #{pid}")
	@Results(id = "product", value = { @Result(column = "cid", property = "category.cid"),
			@Result(column = "cname", property = "category.cname") })
	public Product findByPid2(int pid);

	@Select("select *  from tab_product")
	@ResultMap("productOne")
	public Page<Product> findAll();

	@Select("select tab_product.*,tab_category.cname from tab_product join tab_category on tab_product.cid = tab_category.cid")
	@ResultMap("product")
	public Page<Product> findAll2();
	
	@Select("select tab_product.*,tab_category.cname from tab_product join tab_category on tab_product.cid = tab_category.cid where tab_product.cid = #{cid}")
	@ResultMap("product")
	public Page<Product> findByCid(int cid);
	
	@Select("select *  from tab_product where is_hot =1 and pflag =1")
	public Page<Product> findHot();
	@Select("select *  from tab_product where pflag = 1")
	public Page<Product> findNew();
	
}
